package com.dormitory.javadormitory.controller;

import com.dormitory.javadormitory.common.Result;
import com.dormitory.javadormitory.entity.Bunkhouse;
import com.dormitory.javadormitory.service.ArticleManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@RequestMapping("/Repair")
public class ArticleManageController {

    @Autowired
    private ArticleManageService articleManageService;

    //查询所有报修的问题数据
    @GetMapping("/all")
    public Result all(){
        ArrayList<Bunkhouse> arr =  articleManageService.bunkhouseAll();
        return Result.success(arr);
    }

    //根据id找到要修改的数据
    @GetMapping("/inquireId")
    public Result inquireId(int id){
        Bunkhouse b =  articleManageService.inquireId(id);
        return Result.success(b);
    }

    //根据id修改数据
    @PutMapping("/editId")
    public Result editId(@RequestBody Bunkhouse bunkhouse){
        if ((bunkhouse.getBunkhouseName().equals("")) || (bunkhouse.getIssue().equals(""))){
            return Result.success("名字或报修内容不能为空");
        }
        articleManageService.editId(bunkhouse);
        return Result.success();
    }

    //根据宿舍名称查询
    @GetMapping("/inquireBunkhouseName")
    public Result inquireBunkhouseName(String bunkhouseName){
        ArrayList<Bunkhouse> arr =  articleManageService.bunkhouseName(bunkhouseName);
        //为空判断
        if (arr.size() == 0){
            return Result.success("查询为空");
        }
        return Result.success(arr);
    }

    //添加problemInput
    @PostMapping("/problemInput")
    public Result problemInput(@RequestBody Bunkhouse bunkhouse){
        if ((bunkhouse.getBunkhouseName().equals("")) || (bunkhouse.getIssue().equals(""))){
            return Result.success("名字或报修内容不能为空");
        }
        articleManageService.problemInput(bunkhouse);
        return Result.success();
    }

    //根据id删除
    @GetMapping("/deleteBunkhouse1")
    public Result deleteBunkhouse1(int id){
        articleManageService.deleteBunkhouse1(id);
        return Result.success();
    }

}
